package com.qdlc.p2p.quartz.job;

import com.google.common.base.Joiner;
import com.qd.p2p.adapter.AdaptedRequestHandleService;
import com.qd.p2p.adapter.param.QueryP2PBusinessParameter;
import com.qd.p2p.adapter.response.QueryP2PBusinessResponse;
import com.qd.p2p.baofoo.enums.P2pBizType;
import com.qd.p2p.baofoo.enums.RechargeState;
import com.qdlc.p2p.biz.processor.task.RechargeSuccessTaskProcessor;
import com.qdlc.p2p.biz.service.AccountRechargeService;
import com.qdlc.p2p.common.util.CollectionUtils;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.model.AccountRechargeModel;
import com.qdlc.p2p.quartz.AbstractJob;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by gaoj on 2016/4/27.
 */
public class RechargeStatusInvalid extends AbstractJob {

    @Resource
    private AccountRechargeService accountRechargeService;

    @Resource
    AdaptedRequestHandleService adaptedPayService;

    @Resource
    RechargeSuccessTaskProcessor rechargeSuccessTaskProcessor;

    @Override
    protected void process() {

        //每一分钟查询充值状态为0的订单
        AccountRechargeModel model = new AccountRechargeModel();
        model.setStatus(AccountRechargeModel.RECHARGE_UNTREATED);
        model.setType(99);
        model.setPage(1);
        model.setRows(100);
        model.setSort("addTime");
        model.setOrder("desc");
        PageDataList<AccountRechargeModel> pateDateList = accountRechargeService.list(model);
        List<AccountRechargeModel> accountRechargeModelList = pateDateList.getList();
        if (!CollectionUtils.isNotNullOrEmpty(accountRechargeModelList)) {
            return;
        }

        Set<String> orderNoSet = new HashSet<String>();
        Map<String, AccountRechargeModel> accountRechargeModelMap = new HashMap<String, AccountRechargeModel>();
        //遍历集合，查询宝付接口充值订单的真实状态
        for (AccountRechargeModel accountRechargeModel : accountRechargeModelList) {
            String orderNo = accountRechargeModel.getTradeNo();
            orderNoSet.add(orderNo);
            accountRechargeModelMap.put(orderNo, accountRechargeModel);
        }

        String orderNos = Joiner.on(',').join(orderNoSet);//将订单号用','隔开

        //查询充值订单号状态
        QueryP2PBusinessParameter parameter = new QueryP2PBusinessParameter();
        parameter.setOrderId(orderNos);
        parameter.setType(P2pBizType.RECHARGE.getType());
        QueryP2PBusinessResponse response = adaptedPayService.queryP2PBusiness(parameter);
        List<QueryP2PBusinessResponse.Order> orders = response.getResult();

        //遍历集合，如果与宝付接口充值订单的状态都为1处理成功，否则处理失败
        for (QueryP2PBusinessResponse.Order order : orders) {
            int state = order.getState();
            AccountRechargeModel accountRecharge = accountRechargeModelMap.get(order.getOrderId());
            if (RechargeState.SUCCESS.getState() == state) {
                rechargeSuccessTaskProcessor.doSuccess(accountRecharge.prototype());
            } else {
                Date addTime = accountRecharge.getAddTime();
                Calendar now = Calendar.getInstance();
                now.add(Calendar.HOUR, -1);
                if (now.after(addTime)) {
                    rechargeSuccessTaskProcessor.doFailure(accountRecharge);
                }
            }
        }
    }
}
